import storage from 'store'
import expirePlugin from 'store/plugins/expire'
import { login, getUserInfo, getInfo, logout, getPicBaseUrl, userLogin } from '@/api/index'
import { ACCESS_TOKEN } from '@/store/mutation-types'

storage.addPlugin(expirePlugin)
const user = {
  state: {
    token: '',
    name: '',
    YtAdminUserAliasCH:'',
    avatar: '',
    roles: [],
    info: {},
    baseUrl: '',
    simToken:'',
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name }) => {
      state.name = name
    },
    SET_NAMEAliasCH: (state, { nameAliasCH }) => {
      state.nameAliasCH = nameAliasCH
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
    SET_BASEURL: (state, baseUrl) => {
      state.baseUrl = baseUrl
    },
    //存企业token
    SET_SimToken: (state, simToken) => {
      state.simToken = simToken
    }

  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(({ data }) => {
          storage.set(ACCESS_TOKEN, data.token, new Date().getTime() + 7 * 24 * 60 * 60 * 1000)
          commit('SET_TOKEN', data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 用户账号密码登录
    UserLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        userLogin(userInfo).then(({ data }) => {
          console.log("xxxxxx:", data.token)
          storage.set(ACCESS_TOKEN, data.token, new Date().getTime() + 7 * 24 * 60 * 60 * 1000)
          commit('SET_TOKEN', data.token)
          localStorage.setItem('bmwToken',"dabe69138599589384452ac32a0f43d6")
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    //模拟用户账号密码登录
    simulatelogin({ commit },token){
      return new Promise((resolve, reject) => {
      storage.set(ACCESS_TOKEN, token, new Date().getTime() + 7 * 24 * 60 * 60 * 1000)
      commit('SET_TOKEN', token)
    })
    },

    // 获取用户信息
    GetInfo({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const { data } = await getUserInfo()
          commit('SET_INFO', data)
          commit('SET_NAME', { name: data.name })
          commit('SET_NAMEAliasCH', { nameAliasCH: data.YtAdminUserAliasCH })
          commit('SET_ROLES', [...data.featureAlias])
          data.featureAlias.map((item,index)=>{
            if (item === 'userportal'){
              localStorage.setItem('collapsed',true)
            }
          })
          const { data: baseUrl } = await getPicBaseUrl()
          commit('SET_BASEURL', baseUrl)
          // commit('SET_AVATAR', result.avatar)
          resolve({ role: [...data.featureAlias] })
        } catch (error) {
          reject(error)
        }
      })
    },

    // 登出
    Logout({ commit, state }) {
      return new Promise(async (resolve) => {
        try {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          storage.remove(ACCESS_TOKEN)
          localStorage.removeItem('bmwToken')
          // localStorage.setItem('lang',JSON.stringify("zh-EN"))
          const x = {
            loginout: "true"
          };
          const data = JSON.stringify(x);
          window.parent.postMessage(data, "*");
          resolve()
        } catch (error) {
          console.log(error)
          resolve()
        }
      })
    }
  }
}

export default user
